package com.example.memosystem.service.impl;

import com.example.memosystem.mapper.UserMapper;
import com.example.memosystem.model.User;
import com.example.memosystem.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UserServiceImpl implements UserService {
    
    @Autowired
    private UserMapper userMapper;
    
    @Override
    public boolean register(User user) {
        User existingUser = userMapper.findByUsername(user.getUsername());
        if (existingUser != null) {
            return false;
        }
        
        Date now = new Date();
        user.setCreateTime(now);
        user.setUpdateTime(now);
        return userMapper.insertUser(user) > 0;
    }
    
    @Override
    public User login(String username, String password) {
        User user = userMapper.findByUsername(username);
        if (user != null && user.getPassword().equals(password)) {
            return user;
        }
        return null;
    }

    @Override
    public User findById(Integer id) {
        return userMapper.findById(id);
    }

    @Override
    public boolean updateUser(User user) {
        user.setUpdateTime(new Date());
        return userMapper.updateUser(user) > 0;
    }
}